package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.dto.type.TypeAddRequest;
import com.gzhu.knowledgeAdmin.common.dto.type.TypeQueryRequest;
import com.gzhu.knowledgeAdmin.common.exception.BusinessException;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.service.TemplateTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
public class TemplateTypeController {

    @Autowired
    private TemplateTypeService templateTypeService;

    /**
     * 用户界面调用的接口,返回String类型数组
     * @return
     */
    @GetMapping("/template/type/list")
    public R getTypeList() {
        return R.ok(templateTypeService.getTypeList());
    }

    /**
     * 管理员界面调用的接口
     * @return 返回TemplateType类型的的list集合,(带数据详细信息,eg:id)
     */
    @PostMapping("/template/type/allList")
    public R getAllTypeList(@RequestBody TypeQueryRequest typeQueryRequest) {
        return R.ok(templateTypeService.getAllTypeList());
    }

    @PostMapping("/template/type/add")
    public R addType(@RequestBody TypeAddRequest typeAddRequest) {

        if (StringUtils.isEmpty(typeAddRequest.getName())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateTypeService.addType(typeAddRequest);

        return R.ok(true);
    }

    @PostMapping("/template/type/delete")
    public R deleteType(@RequestBody List<Integer> idList) {

        if (idList.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        templateTypeService.deleteType(idList);

        return R.ok(true);
    }

}
